package com.tsing.cedap.rest.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.tsing.cedap.rest.service.IBannerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author TheTsing
 * @create 2022-02-25 15:03
 */
@Service
public class BannerServiceImpl implements IBannerService {

    @Resource
    private OSS ossClient;

    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endpoint;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucket;

    @Value("${spring.cloud.alicloud.access-key}")
    private String accessId;

    @Override
    public Map<String, String> policy() {
        // 用户上传文件时指定的前缀。这里每天创建一个文件夹
        String dir = "cedap/";
        Map<String, String> respMap = null;
        try {
            Date expire = DateUtil.offsetSecond(new Date(), 30);
            // PostObject请求最大可支持的文件大小为5 GB，即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
            String postPolicy = ossClient.generatePostPolicy(expire, policyConds);
            byte[] binaryData = postPolicy.getBytes("UTF-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);
            respMap = new LinkedHashMap<>();
            respMap.put("accessid", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", "https://" + bucket + "." + endpoint);
            respMap.put("expire", String.valueOf(expire.getTime() / 1000));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        return respMap;
    }

    public void testWebUpload(MultipartFile file) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("key", "dir" + file.getOriginalFilename());
        map.put("policy", "policy");
        map.put("OSSAccessKeyId", "accessid");
        map.put("signature", "signature");
        try {
            map.put("file", FileUtil.writeFromStream(file.getInputStream(), file.getOriginalFilename()));
        } catch (IOException e) {
        }
//        int status = HttpRequest.post(ossPolicyDTO.getHost()).form(map).execute().getStatus();
        int status = 204;
        //post 请求发送到host 获取响应的status
        if (status == 204) {
            String addr = "host" + "/" + "dir" + file.getOriginalFilename();
        } else {
            System.out.println("上传失败");
        }
    }

}
